import QQMapWX from '../../utils/qqmap-wx-jssdk.min';
import { formatTime } from '../../utils/util'
const {
  getMarkingToolInfoApi,
  qrCodeGetQrCodeApi
} = require('../../api/index')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sharePath: "",
    imgDraw: null,
    addressName: "未知",
    lawyerPhoto:"",
    weather: {
      tempMin: "",
      tempMax: ""
    },
    deptName: '',
    lawyerPhosto: '',
    lawyerName: '',
    lawyerId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(optios: any) {
    this.setData({
      systemInfo: wx.getMenuButtonBoundingClientRect(),
      lawyerName: decodeURIComponent(optios.lawyerName),
      lawyerPhoto: decodeURIComponent(optios.lawyerPhoto),
      deptName: decodeURIComponent(optios.deptName),
      lawyerId: optios.lawyerId,

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    let qqMap: any = new QQMapWX({
      key: 'PRDBZ-2BXK3-XOD32-YEYA7-SXDD5-RRBOW' // 必填
    });
    // 获取本地经纬度
    wx.getLocation({
      type: 'wgs84',
      success(locationRes) {
        // 获取当前地址中文名字
        qqMap.reverseGeocoder({
          location: {
            latitude: locationRes.latitude,
            longitude: locationRes.longitude
          },
          success: function (addressRes: any) {
            that.setData({
              addressName: addressRes.result.ad_info.city
            })
            // 获取当前地址天气状况
            wx.request({
              url: `https://devapi.qweather.com/v7/weather/3d?location=${locationRes.longitude},${locationRes.latitude}&key=511d0e3ec6f7463dac3a077ccd9295ec`,
              method: "GET",
              success(res: any) {
                that.setData({
                  weather: res.data.daily[0] || {}
                })

                that.setImgDraw()
              }
            })

          },
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  navigateBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  savePhoto() {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.sharePath,
      success(res) {
        wx.showToast({
          title: '保存成功',
          icon: "success",
          duration: 1000
        })
      }
    })
  },
  onImgOK(e: any) {
    wx.hideLoading()
    this.setData({
      sharePath: e.detail.path,
    })
  },
  previewImg() {
    wx.previewImage({
      current: this.data.sharePath,
      urls: [this.data.sharePath]
    })
  },
  setImgDraw() {
    wx.showToast({
      title: '加载中，请稍后...',
      icon: 'none'
    })

    getMarkingToolInfoApi({
      deptId: true,
      toolType: 1
    }).then(infoRes => {
      qrCodeGetQrCodeApi({
        deptId: true,
        path: infoRes.data.shareType == 2 ? 'pages/lawyer/lawyer?id=' + (infoRes.data.lawyerId || this.data.lawyerId) : 'pages/team/team',
        width: 87,
        lawyerId: infoRes.data.shareType == 2 ? infoRes.data.lawyerId || this.data.lawyerId : undefined,
        qrCodeType: infoRes.data.shareType || 1
      }).then(codeRes => {
        if (infoRes.data.content) {
          let imgDraw: any = {
            "width": "670px",
            "height": "1000px",
            "background": "transparent",
            "views": [
              {
                "type": "image",
                "url": app.globalData.imgSrc || infoRes.data.pictureAddr,
                "css": {
                  "width": "670px",
                  "height": "1000px",
                  "top": "0",
                  "left": "0",
                  "rotate": "0",
                  // "borderRadius": "30px",
                  "borderRadius": "0",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "mode": "scaleToFill"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "rgba(0,0,0,0.6)",
                  "width": "670px",
                  "height": "1000px",
                  "top": "0px",
                  "left": "0px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "rgba(0,0,0,0.2)"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#FF8686",
                  "width": "80px",
                  "height": "6px",
                  "top": "80px",
                  "left": "40px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#FF8686"
                }
              },
              {
                "type": "text",
                "text": "早安",
                "css": {
                  "color": "#fff",
                  "background": "rgba(0,0,0,0)",
                  "width": "100px",
                  "height": "45.199999999999996px",
                  "top": "40px",
                  "left": "40px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "40px",
                  "fontWeight": "normal",
                  "maxLines": "1",
                  "lineHeight": "44.400000000000006px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "left"
                }
              },
              {
                "type": "text",
                "text": formatTime(new Date()).substr(0, 10),
                "css": {
                  "color": "rgba(255,255,255,0.6)",
                  "background": "",
                  "width": "200px",
                  "height": "24.86px",
                  "top": "96px",
                  "left": "40px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "22px",
                  "fontWeight": "normal",
                  "maxLines": "1",
                  "lineHeight": "24.42px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "left"
                }
              },

              {
                "type": "image",
                "url": this.data.lawyerPhoto,
                "css": {
                  "width": "130px",
                  "height": "120px",
                  "top": "840px",
                  "left": "32px",
                  "rotate": "0",
                  "borderRadius": "10px",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "mode": "scaleToFill"
                }
              },
              {
                "type": "text",
                "text": this.data.lawyerName + " 律师",
                "css": {
                  "color": "#fff",
                  "background": "",
                  "width": "300px",
                  "height": "31.639999999999997px",
                  "top": "870px",
                  "left": "183px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "28px",
                  "fontWeight": "normal",
                  "maxLines": "1",
                  "lineHeight": "31.080000000000002px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "left"
                }
              },
              {
                "type": "text",
                "text": this.data.deptName,
                "css": {
                  "color": "rgba(255,255,255,0.6)",
                  "background": "",
                  "width": "300px",
                  "height": "50px",
                  "top": "912px",
                  "left": "183px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "20px",
                  "fontWeight": "normal",
                  "maxLines": "2",
                  "lineHeight": "25px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "left"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "",
                  "width": "120px",
                  "height": "120px",
                  "top": "818px",
                  "left": "512px",
                  "rotate": "0",
                  "borderRadius": "0.5px",
                  "borderWidth": "1px",
                  "borderColor": "rgba(255,255,255,0.2)",
                  "shadow": "",
                  "color": "transparent"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "14px",
                  "height": "1px",
                  "top": "816px",
                  "left": "510px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "14px",
                  "height": "1px",
                  "top": "939px",
                  "left": "510px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "1px",
                  "height": "14px",
                  "top": "816px",
                  "left": "510px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "1px",
                  "height": "14px",
                  "top": "925px",
                  "left": "510px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "1px",
                  "height": "14px",
                  "top": "925px",
                  "left": "633px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "14px",
                  "height": "1px",
                  "top": "939px",
                  "left": "620px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "1px",
                  "height": "14px",
                  "top": "817px",
                  "left": "633px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "#eee",
                  "width": "14px",
                  "height": "1px",
                  "top": "816px",
                  "left": "620px",
                  "rotate": "0",
                  "borderRadius": "",
                  "shadow": "",
                  "color": "#eee"
                }
              },
              {
                "type": "image",
                "url": codeRes.data.qrCodeAddr,
                "css": {
                  "width": "104px",
                  "height": "104px",
                  "top": "826px",
                  "left": "520px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "mode": "scaleToFill"
                }
              },
              {
                "type": "text",
                "text": "微信扫码 关注我们",
                "css": {
                  "color": "#fff",
                  "background": "",
                  "width": "120px",
                  "height": "15.819999999999999px",
                  "top": "945px",
                  "left": "513px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "14px",
                  "fontWeight": "normal",
                  "maxLines": "1",
                  "lineHeight": "15.540000000000001px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "center"
                }
              },
              {
                "type": "rect",
                "css": {
                  "background": "",
                  "width": "131px",
                  "height": "36px",
                  "top": "52px",
                  "left": "499px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "1px",
                  "borderColor": "rgba(255,255,255,0.3)",
                  "shadow": "",
                  "color": "transparent"
                }
              },
              {
                "type": "text",
                "text": `${this.data.addressName.substr(0, 2)}·${this.data.weather.tempMin}-${this.data.weather.tempMax}°C`,
                "css": {
                  "color": "rgba(255,255,255,0.8)",
                  "background": "",
                  "width": "131px",
                  "height": "20.339999999999996px",
                  "top": "59px",
                  "left": "499px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "18px",
                  "fontWeight": "normal",
                  "maxLines": "1",
                  "lineHeight": "19.98px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "center"
                }
              },
              {
                "type": "image",
                "url": "https://file.tinglifangs.com/pro/other/1678782534567.png",
                "css": {
                  "width": "32px",
                  "height": "32px",
                  "top": "50px",
                  "left": "128px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "mode": "scaleToFill"
                }
              },
              {
                "type": "text",
                "text": infoRes.data.content,
                "css": {
                  "color": "#fff",
                  "background": "",
                  "width": "590px",
                  "height": "586.04px",
                  "top": "208px",
                  "left": "40px",
                  "rotate": "0",
                  "borderRadius": "",
                  "borderWidth": "",
                  "borderColor": "",
                  "shadow": "",
                  "padding": "0px",
                  "fontSize": "30px",
                  "fontWeight": "normal",
                  "maxLines": "9",
                  "lineHeight": "45px",
                  "textStyle": "fill",
                  "textDecoration": "none",
                  "fontFamily": "",
                  "textAlign": "left"
                }
              }
            ]
          }
          this.setData({
            imgDraw
          })
          app.globalData.imgSrc = null;
        } else {
          wx.showToast({
            icon: 'none',
            title: '请前往后台增加内容'
          })
        }
      })
    })
  },
  changeImage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        wx.navigateTo({
          url: `/pages/cropper/cropper?imgSrc=${res.tempFiles[0].tempFilePath}&height=447`
        })
      }
    })
  }
})